//
//  UIColor+Categories.m
//  BBUniversalFramework
//
//  Created by Mibao12 on 2020/2/10.
//  Copyright © 2020 半饱. All rights reserved.
//

#import "NSObject+BBAlertCategories.h"

@implementation UIColor(BBAlertCategories)

+ (UIColor *)colorWithHex:(long)hexColor {
    return [UIColor colorWithHex:hexColor alpha:1.f];
}

+ (UIColor *)colorWithHex:(long)hexColor alpha:(CGFloat)alpha {
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (UIColor *)randomColor {
    CGFloat hue = (arc4random() % 256 / 256.0);
    CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5;
    CGFloat brightness = (arc4random() % 128 / 256.0) + 0.5;
    UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    return color;
}

+ (UIColor *)dynamicColorWithDefaultColor:(NSUInteger)defaultColor withDarkColor:(NSUInteger)darkColor {
    if (@available(iOS 13.0, *)) {
        UIColor *resultColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if ([traitCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
                return [UIColor colorWithHex:defaultColor];
            } else {
                return [UIColor colorWithHex:darkColor];
            }
        }];
        return resultColor;
    } else {
        return [UIColor colorWithHex:defaultColor];
    }
}
@end

@implementation UIButton (BBAlertCategories)

- (void)buttonWithEdgeInsetsStyle:(BBAlertActionEdgeInsetsStyle)edgeInsetsStyle withImageTitleSpace:(CGFloat)imageTitleSpace {
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWidth = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = self.titleLabel.intrinsicContentSize.width;
    CGFloat labelHeight = self.titleLabel.intrinsicContentSize.height;
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    switch (edgeInsetsStyle) {
        case BBAlertActionEdgeInsetsStyleTop: {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-imageTitleSpace/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight-imageTitleSpace/2.0, 0);
        }
            break;
        case BBAlertActionEdgeInsetsStyleLeft: {
            imageEdgeInsets = UIEdgeInsetsMake(0, -imageTitleSpace/2.0, 0, imageTitleSpace/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, imageTitleSpace/2.0, 0, -imageTitleSpace/2.0);
        }
            break;
        case BBAlertActionEdgeInsetsStyleBottom:  {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-imageTitleSpace/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-imageTitleSpace/2.0, -imageWidth, 0, 0);
        }
            break;
        case BBAlertActionEdgeInsetsStyleRight: {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+imageTitleSpace/2.0, 0, -labelWidth-imageTitleSpace/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-imageTitleSpace/2.0, 0, imageWidth+imageTitleSpace/2.0);
        }
            break;
        default:
            break;
    }
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
    self.bounds = CGRectMake(0,0,self.frame.size.width+imageTitleSpace, self.frame.size.height);
}
@end

@implementation UIImage (BBCategories)

- (UIImage*)imageScaleWithSize:(CGSize)size {
    if (size.width < 1 || size.height < 1) {
        return nil;
    }
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

@end
